<?php


namespace app\admin\common;


use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Export
{
    function exportExcel($expTitle,$expCellName,$expTableData){
        set_time_limit(0);
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        $objPHPExcel = new Spreadsheet();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
//        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');
//        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' 导出时间:'.date('Y-m-d H:i:s'));
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        $filePath = str_replace('\\', '/', $expTitle . '.xlsx');
        $dir = "uploads/excel/";
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        ob_clean();
        ob_start();
        $objWriter = new Xlsx($objPHPExcel);
        $objWriter->save($dir . $filePath);
        /* 释放内存 */
        $objPHPExcel->disconnectWorksheets();
        unset($objSpreadsheet);
        ob_end_flush();
        $response = array(
            'code' => 200,
            'success' => true,
             'url' => "http://" . $_SERVER['SERVER_NAME'] . "/" . $dir . $filePath
        );

        return $response;
    }
}